package com.ruoyi.jpjstb.service.param.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.jpjstb.domain.common.Dept;
import com.ruoyi.jpjstb.domain.common.HrSalary;
import com.ruoyi.jpjstb.domain.common.Month;
import com.ruoyi.jpjstb.domain.common.TwoDept;
import com.ruoyi.jpjstb.mapper.SalaryMapper;
import com.ruoyi.jpjstb.service.SalaryService;
import com.ruoyi.jpjstb.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SalaryServiceimpl implements SalaryService {
    @Autowired
    SalaryMapper salaryMapper;


    @Override
    public List<HrSalary> selectSalaryList(HrSalary hrSalary) {
        return salaryMapper.selectSalaryList(hrSalary);
    }

    @Override
    public int deleteSalaryByIds(String ids) {
        return salaryMapper.deleteSalaryByIds(Convert.toStrArray(ids));
    }


    @Override
    public int insertSalary(HrSalary hrSalary) {
        return salaryMapper.insertSalary(hrSalary);
    }

    @Override
    public HrSalary selectSalaryById(Long id) {
        return salaryMapper.selectSalaryById(id);
    }

    @Override
    public int updateSalary(HrSalary hrSalary) {
        return salaryMapper.updateSalary(hrSalary);
    }


    public List<Dept> findDept(Dept dept) {

        return salaryMapper.findDept(dept);
    }

    public List<TwoDept> findTwoDept(TwoDept twoDept) {

        return salaryMapper.findTwoDept(twoDept);
    }

    public List<Month> findmonth(Month month) {

        return salaryMapper.findmonth(month);
    }

    @Override
    public int importData(MultipartFile file) {
        //获取Excel数据  0节点为所有项目名 为String数组
        int num = 0;
        List<String[]> excelData = ExcelUtils.getExcelData(file);
        Map<String, String> fieldMap = new HashMap<String, String>();
        fieldMap.put("工号", "jobNumber");
        fieldMap.put("姓名", "fullName");
        fieldMap.put("入职日期", "dateEntry");
        fieldMap.put("工资总额", "totalWages");
        fieldMap.put("其它补助", "otherSubsidies");
        fieldMap.put("税前扣款", "taxDeduction");
        fieldMap.put("社保个人", "socialSecurityIndividual");
        fieldMap.put("公积金个人", "accumulationFund");
        fieldMap.put("个税", "personalIncomeTax");
        fieldMap.put("其它扣款", "otherDeductions");
        fieldMap.put("实发金额", "actualAmount");
        fieldMap.put("一级部门", "dept");
        fieldMap.put("二级部门", "twoDept");
        fieldMap.put("月份", "month");


        for (int i = 1; i < excelData.size(); i++) {
            Map<String, Object> addMap = new HashMap<String, Object>();
            for (int j = 0; j < excelData.get(i).length; j++) {
                String val = excelData.get(i)[j];
                //取出对应的字段
                String field = excelData.get(0)[j];
                if (field.equals("电话")) {
                    if (!val.equals("")) {
                        val = val.replace(",", "");
                    }
                }
                addMap.put(fieldMap.get(field), val);
            }
            num += salaryMapper.insertData(addMap);
        }
        return num;
    }
}
